extends Node2D

const Drama = "res://World/剧情文字.tscn"
const Menu = "res://World/Level/Level1穿越.tscn"
const Level1 = "res://World/Level/Level1穿越.tscn"
const Level2 = "res://World/Test/WorldTest0.tscn"
const Level3 = "res://World/Level/Level1穿越.tscn"
const Level4 = "res://World/Level/Level1穿越.tscn"
const Level5 = "res://World/Level/Level1穿越.tscn"
const Level6 = "res://World/Level/Level1穿越.tscn"

onready var load_scene_animation = $LoadSceneAnimation
onready var texture_progress = $CanvasLayer/TextureProgress

var to_scene = ""

func to_level1():
	to_scene = Level1
	


func reload_scene(speed=1):
	get_tree().reload_current_scene()
	var tex = capture_screen()
	texture_progress.texture_progress = tex
	
	texture_progress.rect_position.x = get_viewport().get_visible_rect().size.x / 2 - texture_progress.texture_progress.get_size().x / 2
	
	
	if load_scene_animation.is_playing():
		load_scene_animation.stop()
	load_scene_animation.play("load_scene", -1, speed)

func load_scene(scene_name: String, speed=1):
	Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
	get_tree().change_scene(scene_name)
	var tex = capture_screen()
	texture_progress.texture_progress = tex
	
	texture_progress.rect_position.x = get_viewport().get_visible_rect().size.x / 2 - texture_progress.texture_progress.get_size().x / 2
	
	if load_scene_animation.is_playing():
		load_scene_animation.stop()
	load_scene_animation.play("load_scene", -1, speed)
	# yield(load_scene_animation, "animation_finished")
	
func load_scene_to(scene: PackedScene, speed=1):
	Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
	get_tree().change_scene_to(scene)
	var tex = capture_screen()
	texture_progress.texture_progress = tex
	
	texture_progress.rect_position.x = get_viewport().get_visible_rect().size.x / 2 - texture_progress.texture_progress.get_size().x / 2
	
	if load_scene_animation.is_playing():
		load_scene_animation.stop()
	load_scene_animation.play("load_scene", -1, speed)
	# yield(load_scene_animation, "animation_finished")

func capture_screen() -> ImageTexture:
	# Retrieve the captured image.
	var img = get_viewport().get_texture().get_data()

	# Flip it on the y-axis (because it's flipped).
	img.flip_y()

	# Create a texture for it.
	var tex = ImageTexture.new()
	tex.create_from_image(img)
	
	return tex

func _input(event: InputEvent) -> void:
	if event.is_action_pressed("restart"):
		reload_scene()
